const conn = require('../data/yyl_system');
module.exports.yylsystem = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    res.render('yyl_system');
}
// 全部员工
module.exports.yylsystemqbyg = (req, res) => {
    conn.query('select *  from `use` where isDel <> "true"', (err, data) => {
        // console.log(data);
        if (err) {
            return console.log(err);
        }
        // res.json(data);
        res.send(data);
    })
}

// 添加数据模板
module.exports.signin = (req, res) => {
    conn.query('', (err, data) => {
        if (err) {
            return console.log(err);
        }
        res.send(data);
    })
}
// 添加数据
module.exports.useradda = (req, res) => {
    // console.log(req.body);
    let userdata = [req.body.username, req.body.name, req.body.sex, req.body.title, req.body.department, req.body.rightgroup, req.body.lastlogin, req.body.states];
    conn.query('insert into `use`(username,name,sex,title,department,rightgroup,lastlogin,states)values(?,?,?,?,?,?,?,?)', userdata, (err, data) => {

        if (err) {
            return console.log(err);
        }
        res.json({
            code: '100',
            msg: '添加成功'
        })
    })
}

module.exports.delusera = (req, res) => {
    console.log(req.query);
    conn.query('select * from `use` where isDel = "true"', (err, data) => {
        if (err) return console.log(err);
        res.json(data);

    })
}
// 彻底删除
// 数据放到回收站
module.exports.delusers = (req, res) => {
    console.log(req.query);
    conn.query('UPDATE `use` SET isDel = "true" WHERE isDel = "false" and id=?', [req.query.dataid], (err, data) => {
        if (err) return console.log(err);
        if (data.affectedRows) (
            res.json({
                code: '101',
                msg: '删除成功'
            })
        )

    })
}
// 回收站还原
module.exports.reduction = (req, res) => {
    console.log(req.query);
    conn.query('UPDATE `use` SET isDel = "false" WHERE isDel = "true" and id=?', [req.query.dataid], (err, data) => {
        if (err) return console.log(err);
        if (data.affectedRows) (
            res.json({
                code: '101',
                msg: '还原成功'
            })
        )

    })
}

// // 信息回显
// module.exports.userFind = (req, res) => {
//     console.log(req.query);
//     conn.query('select * from `use` where id=?', [req.query.id], (err, data) => {
//         if (err) return console.log(err);
//         res.json(data);

//     })
// }
// // 更新数据
// module.exports.userUpdate = (req, res) => {
//     let sql = 'update `use` set username=?, name=?, sex=?,title=?, department=?,rightgroup=?,lastlogin=?,states=? where id=?';
//     let userdata = [req.body.username, req.body.name, req.body.sex, req.body.title, req.body.department, req.body.rightgroup, req.body.lastlogin, req.body.states, req.body.id];
//     console.log(req.body);
//     // console.log(userdata);
//     conn.query(sql, userdata, (error, results) => {
//         if (error) return console.log(error);

//         //   console.log(results);
//         if (results.affectedRows) {
//             res.json({
//                 code: '102',
//                 message: '用户信息更新成功'
//             })
//         }
//     })
// }
// 查询数据
module.exports.userechoa = (req, res) => {
    conn.query('select * from `use` where name like "%' + req.query.iconname + '%" and states in' + req.query.states, (eer, results) => {
        if (eer) return console.log(eer);
        res.json(results);
    })
}
//  禁用员工渲染
module.exports.yylsystemjyyg = (req, res) => {
    conn.query('select * from `use` where states = "禁用" and isDel <> "true"', (err, data) => {
        if (err) {
            return console.log(err);
        }
        // res.json(data);
        res.send(data);
    })
}
// 正常员工
module.exports.yylsystemzcyg = (req, res) => {
    conn.query('select * from `use` where states = "正常" and isDel <> "true"', (err, data) => {
        if (err) {
            return console.log(err);
        }
        // res.json(data);
        res.send(data);
    })
}
// 按钮禁用员工
module.exports.jyuser = (req, res) => {
    // console.log(req.query);
    console.log(req.query.isJin);
    let target = req.query.isJin == '正常' ? '正常' : '禁用';
    conn.query('UPDATE `use` SET states = ? WHERE id=?', [target, req.query.id], (err, data) => {
        if (err) return console.log(err);
        // if (data.affectedRows) (
        res.json({
            code: '103',
            msg: '已成功改为' + target + '状态'
        })
        // )
        // console.log(data);

    })
} 